package com.huawei.leetcode;

public class Code14 {
    public static void main(String[] args) {
        String[] strs = {"dog","racecar","car"};
        System.out.println(new Code14().longestCommonPrefix(strs));
    }

    public String longestCommonPrefix(String[] strs) {
        String heatStr = strs[0];
        String ans = heatStr;
        for (int i = heatStr.length(); i >= 0; i--) {
            for (int j = 1; j < strs.length; j++) {
                String indexString = strs[j];
                String substring = heatStr.substring(0, i);
                if (indexString.length() >= substring.length()
                        && indexString.startsWith(substring)) {
                    ans = ans.length() < substring.length() ? ans : substring;
                    if(j>=strs.length-1){
                        return ans;
                    }
                } else {
                    break;
                }
            }
        }
        return ans;
    }
}
